package com.xy.lang.util;

/**
 * 脱敏工具
 */
public class DesensitizeUtils {

    private DesensitizeUtils() {}

    /**
     * 手机号格式校验正则
     */
    public static final String PHONE_VALIDATE_REGEX = "^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$";

    /**
     * 手机号脱敏筛选正则
     */
    public static final String PHONE_DESENSITIZE_REGEX = "(\\d{3})\\d{4}(\\d+)";

    /**
     * Email脱敏筛选正则
     */
    public static final String EMAIL_DESENSITIZE_REGEX = "^(\\w)[^@]*(@.*$)";

    /**
     * 身份证脱敏筛选正则
     */
    public static final String ID_NUMBER_DESENSITIZE_REGEX = "(\\w{6})\\w*(\\w{3})";

    /**
     * 脱敏替换正则
     */
    public static final String DESENSITIZE_REPLACE_REGEX = "$1****$2";

    /**
     * 手机号格式校验
     *
     * @param phone
     * @return
     */
    public static final boolean checkPhone(String phone) {
        if (null == phone || "".equals(phone.trim())) {
            return false;
        }
        return phone.matches(PHONE_VALIDATE_REGEX);
    }

    /**
     * 手机号脱敏处理
     *
     * @param phone
     * @return
     */
    public static final String desensitizePhone(String phone) {
        return desensitizePhone(phone, true);
    }

    /**
     * 手机号脱敏处理
     *
     * @param phone
     * @return
     */
    public static final String desensitizePhone(String phone, boolean validate) {
        if (validate && !checkPhone(phone)) {
            throw new IllegalArgumentException("手机号格式不正确!");
        }
        if (null == phone || "".equals(phone.trim())) {
            return "";
        }
        return phone.replaceAll(PHONE_DESENSITIZE_REGEX, DESENSITIZE_REPLACE_REGEX);
    }

    /**
     * Email脱敏处理
     *
     * @param email
     * @return
     */
    public static final String desensitizeEmail(String email) {
        if (null == email || "".equals(email.trim())) {
            return "";
        }
        return email.replaceAll(EMAIL_DESENSITIZE_REGEX, DESENSITIZE_REPLACE_REGEX);
    }

    /**
     * 名字脱敏处理
     *
     * @param name
     * @return
     */
    public static String desensitizeName(String name){
        if (null == name || "".equals(name)) {
            return "";
        }
        if(name.length() == 2){
            return "*" + name.substring(1);
        } else if(name.length() == 3){
            return name.substring(0, 1) + "*" + name.substring(2);
        } else if(name.length() > 3 && name.length() <= 6){
            return "**" + name.substring(2);
        } else if(name.length() > 6){
            return name.substring(0, 2) + "****" + name.substring(6);
        }
        return "*";
    }

    /**
     * 身份证脱敏处理
     *
     * @param idNumber
     * @return
     */
    public static String desensitizeIdNumber(String idNumber){
        if (null == idNumber || "".equals(idNumber)) {
            return "";
        }
        if (idNumber.length() == 15){
            idNumber = idNumber.replaceAll(ID_NUMBER_DESENSITIZE_REGEX, "$1******$2");
        } else if (idNumber.length() == 18){
            idNumber = idNumber.replaceAll(ID_NUMBER_DESENSITIZE_REGEX, "$1*********$2");
        }
        return idNumber;
    }



    /// tests

    public static void main(String[] args) {
        System.out.println(desensitizePhone("15010168082"));
        System.out.println(desensitizePhone(null, false));
        System.out.println(desensitizePhone("150101", false));
        System.out.println(desensitizePhone("1501016808", false));
        System.out.println(desensitizePhone("80884566", false));
        System.out.println(desensitizeEmail(null));
        System.out.println(desensitizeEmail("name@email.com"));
        System.out.println(desensitizeName("猪"));
        System.out.println(desensitizeName("猪八"));
        System.out.println(desensitizeName("猪八戒"));
        System.out.println(desensitizeName("猪八戒二"));
        System.out.println(desensitizeName("猪八戒二师"));
        System.out.println(desensitizeName("猪八戒二师兄"));
        System.out.println(desensitizeName("猪八戒二师兄打妖怪"));
        System.out.println(desensitizeIdNumber("150105198505121234"));
        System.out.println(desensitizeIdNumber("150105198505121"));
    }

}
